package com.inspectortime.webapp.inspector.fee;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.domain.pricing.PricingPlanType;
import com.inspectortime.repository.Repository;
import com.inspectortime.webapp.util.HttpSessionUtils;

public class PriceHomeController implements Controller {

	private Repository repository;
	
	public void setRepository(Repository repository) {
		this.repository = repository;
	}

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		if (request.getMethod().equalsIgnoreCase("post")) {
			throw new RuntimeException("POST not allowed to this controller");
		}
		
		ModelAndView mv = new ModelAndView("inspector/fee/priceHome");

		InspectorCompany company = HttpSessionUtils.retrieveLoggedInCompany(request, repository);
		mv.addObject("company", company);
		mv.addObject("calculators", company.getFeeCalculators());

		
		// Lazily default every company to BasicFlatFee
		mv.addObject("pricingPlanTypes", PricingPlanType.values());
		if (company.getPricingPlanType() == null) {
			company.setPricingPlanType(PricingPlanType.BasicFlatFee);
			company.save();
		}
		
		return mv;
	}

}
